|
1
|
|
|
var CACHE = 'v05'; |
|
2
|
|
|
|
|
3
|
|
|
self.addEventListener('install', function (evt) { |
|
|
|
|
|
|
4
|
|
|
console.log('The service worker is being installed.'); |
|
|
|
|
|
|
5
|
|
|
evt.waitUntil(caches.open(CACHE).then(function (cache) { |
|
|
|
|
|
|
6
|
|
|
cache.addAll([ |
|
7
|
|
|
'', |
|
8
|
|
|
'index.html', |
|
9
|
|
|
'app.js', |
|
10
|
|
|
'logo.svg', |
|
11
|
|
|
'fonts/*' |
|
12
|
|
|
]); |
|
13
|
|
|
})); |
|
14
|
|
|
}); |
|
15
|
|
|
|
|
16
|
|
|
self.addEventListener('fetch', function (evt) { |
|
|
|
|
|
|
17
|
|
|
if (/\/browser-sync\//.exec(evt.request.url) !== null) { |
|
18
|
|
|
return; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
if (/meshviewer\.json/.exec(evt.request.url) !== null) { |
|
22
|
|
|
// foo |
|
23
|
|
|
} |
|
24
|
|
|
evt.respondWith(fromCache(evt.request)); |
|
25
|
|
|
evt.waitUntil( |
|
26
|
|
|
update(evt.request) |
|
27
|
|
|
.then(refresh) |
|
28
|
|
|
); |
|
29
|
|
|
}); |
|
30
|
|
|
|
|
31
|
|
|
function fromCache(request) { |
|
32
|
|
|
return caches.open(CACHE).then(function (cache) { |
|
|
|
|
|
|
33
|
|
|
return cache.match(request); |
|
34
|
|
|
}); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
function update(request) { |
|
38
|
|
|
return caches.open(CACHE).then(function (cache) { |
|
|
|
|
|
|
39
|
|
|
return fetch(request).then(function (response) { |
|
40
|
|
|
return cache.put(request, response.clone()).then(function () { |
|
41
|
|
|
return response; |
|
42
|
|
|
}); |
|
43
|
|
|
}); |
|
44
|
|
|
}); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Sends a message to the clients. |
|
48
|
|
|
function refresh(response) { |
|
49
|
|
|
return self.clients.matchAll().then(function (clients) { |
|
|
|
|
|
|
50
|
|
|
clients.forEach(function (client) { |
|
51
|
|
|
// Encode which resource has been updated. By including the |
|
52
|
|
|
// [ETag](https://en.wikipedia.org/wiki/HTTP_ETag) the client can |
|
53
|
|
|
// check if the content has changed. |
|
54
|
|
|
var message = { |
|
55
|
|
|
type: 'refresh', |
|
56
|
|
|
url: response.url, |
|
57
|
|
|
// Notice not all servers return the ETag header. If this is not |
|
58
|
|
|
// provided you should use other cache headers or rely on your own |
|
59
|
|
|
// means to check if the content has changed. |
|
60
|
|
|
eTag: response.headers.get('ETag') |
|
61
|
|
|
}; |
|
62
|
|
|
// Tell the client about the update. |
|
63
|
|
|
client.postMessage(JSON.stringify(message)); |
|
64
|
|
|
}); |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
self.addEventListener('activate', function (event) { |
|
|
|
|
|
|
69
|
|
|
var cacheWhitelist = [CACHE]; |
|
70
|
|
|
|
|
71
|
|
|
event.waitUntil( |
|
72
|
|
|
caches.keys().then(function (keyList) { |
|
|
|
|
|
|
73
|
|
|
return Promise.all(keyList.map(function (key) { |
|
74
|
|
|
if (cacheWhitelist.indexOf(key) === -1) { |
|
75
|
|
|
return caches.delete(key); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
return false; |
|
78
|
|
|
})); |
|
79
|
|
|
}) |
|
80
|
|
|
); |
|
81
|
|
|
}); |
|
82
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.